package com.purong.purong.widget;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;

import com.purong.purong.R;
import com.purong.purong.adapter.MaterChoseDialogAdapter;
import com.purong.purong.bean.BankLoanMaterItem;
import com.purong.purong.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zengwenjie on 2017/11/23.
 */
public class MaterChoseDialog extends Dialog {
    private RecyclerView recyclerView;
    private MaterChoseDialogAdapter mAdapter;
    private List<BankLoanMaterItem> itemList=new ArrayList<>();
    private Activity mActivity;
    private OnMaterChoseListener mListener;
    public MaterChoseDialog(Activity context) {
        super(context, R.style.DialogTransparent);
        this.mActivity=context;
        setCanceledOnTouchOutside(true);
        Window window = this.getWindow();
        window .setGravity( Gravity.BOTTOM);
        window.setWindowAnimations(R.style.dialogWindowAnim);
    }
    public void setOnMaterChoseListener(OnMaterChoseListener listener){
        this.mListener=listener;
    }
    public void setData(List<BankLoanMaterItem> itemList){
        this.itemList.clear();
        this.itemList.addAll(itemList);
    }

    @Override
    public void show() {
        super.show();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.dialog_mater_chose_list, null);
        setContentView(inflate);
        findViewById(R.id.main_lay).setLayoutParams(new FrameLayout.LayoutParams(CommonUtil.getScreenWidth(mActivity),CommonUtil.getScreenHeight(mActivity)/4*3));
        findViewById(R.id.empty_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
//        RelativeLayout recyclerLay=(RelativeLayout)  inflate.findViewById(R.id.recycler_lay);
//        recyclerLay.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,CommonUtil.getScreenHeight(mActivity)/4*1));
        recyclerView=(RecyclerView) inflate.findViewById(R.id.recycler_view);
        mAdapter=new MaterChoseDialogAdapter(mActivity);
        mAdapter.setData(itemList);;
        mAdapter.setOnMaterClickListener(new MaterChoseDialogAdapter.OnMaterClickListener() {
            @Override
            public void onClick(int position) {
                if (mListener!=null){
                    mListener.onChose(position,itemList.get(position));
                }
                dismiss();
            }
        });
        recyclerView.setLayoutManager(new FullyLinearLayoutManager(mActivity,LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(mAdapter);



    }

    public interface OnMaterChoseListener{
        void onChose(int position,BankLoanMaterItem item);
    }
}
